package com.cube.dp.cor.filter;

import com.cube.dp.base.error.CommonApiResultCode;
import com.cube.dp.base.error.CommonBuException;
import com.cube.dp.cor.context.TransformContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author cube.li
 * @date 2021/12/13 21:34
 * <p>
 * 短链转换黑名单过滤器
 */
@Component
@Slf4j
public class BlackIpTransformFilter implements TransformFilter {

    /**
     * ip黑名单,这里固定写死
     */
    private static final Set<String> IP_SET = new HashSet<>();

    static {
        IP_SET.add("113.96.233.143");
    }

    @Override
    public void doFilter(TransformContext context) {
        log.debug("ip黑名单过滤...");
        if (IP_SET.contains(context.getIp())) {
            throw new CommonBuException(CommonApiResultCode.BLACK_IP);
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
